package cn.com.session;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "getSessionServlet", value = "/getSessionServlet")
public class getSessionServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session
        HttpSession session = request.getSession();
        //调用session对象的api方法
        String id = session.getId();

        // 获取session的最大非活动时间间隔（秒）
        int maxInactiveInterval = session.getMaxInactiveInterval();
        // 获取session的创建时间（毫秒时间戳）
        long creationTime = session.getCreationTime();
        // 获取session最后一次被访问的时间（毫秒时间戳）
        long lastAccessedTime = session.getLastAccessedTime();
        // 获取session关联的servlet上下文
        ServletContext servletContext = session.getServletContext();
        // 判断当前session是否为新创建的session
        boolean aNew = session.isNew();

        //强制session对象失效
        session.invalidate();

        PrintWriter writer = response.getWriter();

        writer.println("id = " + id);
        writer.println("maxInactiveInterval = " + maxInactiveInterval);
        writer.println("creationTime = " + creationTime);
        writer.println("lastAccessedTime = " + lastAccessedTime);
        writer.println("servletContext = " + servletContext);
        writer.println("aNew = " + aNew);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
